package com.yk.silence.rxmvp.base;

import com.yk.silence.rxmvp.net.api.APIServer;
import com.yk.silence.rxmvp.net.retrofit.RetrofitUtils;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;

public class BasePresenter<V extends BaseView> {

    protected APIServer apiServer = RetrofitUtils.getInstance().getApiServer();
    private CompositeDisposable disposable;
    public V mView;

    public BasePresenter(V baseView) {
        this.mView = baseView;
    }

    /**
     * 解除绑定
     */
    public void detachView() {
        if (mView != null) {
            mView = null;
        }
        removeDisposable();
    }

    /**
     * 获取View
     *
     * @return
     */
    public V getBaseView() {
        return mView;
    }

    /**
     * 添加
     * @param observable
     * @param observer
     */
    public void addDisposable(Observable<?> observable, BaseObserver observer) {
        if (disposable == null) {
            disposable = new CompositeDisposable();
        }
        disposable.add(observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(observer));
    }

    /**
     * 移除
     */
    public void removeDisposable() {
        if (disposable != null) {
            disposable.dispose();
        }
    }
}
